package com.bjsxt.userscenter.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.userscenter.common.controller.BaseController;
import com.bjsxt.userscenter.common.util.ConstatFinalUtil;
import com.bjsxt.userscenter.common.util.EncryptUtil;
import com.bjsxt.userscenter.common.util.PageInfoUtil;
import com.bjsxt.userscenter.users.pojo.AAdmins;
import com.bjsxt.userscenter.users.service.IUsersService;

/**
 * 管理员的操作Controller
 * 
 * @author wangshSxt
 *
 */
@Controller
@RequestMapping("/back/admins/")
public class AdminsBackController extends BaseController
{
	@Resource
	private IUsersService usersService;
	
	/**
	 * 登陆后的首页
	 * @return
	 */
	@RequestMapping("/main")
	public String main()
	{
		ConstatFinalUtil.LOGGER.info("---- main ----");
		return "/back/admins/main" ; 
	}
	
	/**
	 * 左侧菜单切换
	 * @return
	 */
	@RequestMapping("/leftMenu")
	public String leftMenu()
	{
		ConstatFinalUtil.LOGGER.info("---- leftMenu ----");
		return "/back/admins/leftMenu" ; 
	}
	
	/**
	 * 管理员列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/adminsList")
	public String adminsList(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("---- adminsList ----");
		/* 接收搜索条件 */
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		/* 处理搜索条件 */
		if(keyword == null)
		{
			keyword = ""; 
		}
		keyword = keyword.trim() ; 
		if(status == null)
		{
			status = ""; 
		}
		if(st == null)
		{
			st = ""; 
		}
		if(ed == null)
		{
			ed = ""; 
		}
		
		Date stDate = null ; 
		Date edDate = null ; 
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateFormatUtil.parse(st);
			edDate = this.dateFormatUtil.parse(ed);
		}
		
		/* 查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyword", keyword);
		if(!"".equalsIgnoreCase(status))
		{
			condMap.put("status", status);
		}
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		
		/* 分页 */
		PageInfoUtil pageInfoUtil = proccedPageInfo(request);
		
		/* 查询数据 */
		List<AAdmins> adminsList = this.usersService.findCondListAdminsService(pageInfoUtil, condMap);
		/* 将查询的数据放到四大作用域 */
		request.setAttribute("adminsList", adminsList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		
		/* 存储条件 */
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		return "/back/admins/adminsList" ; 
	}
	
	/**
	 * 打开管理员添加页面
	 * @return
	 */
	@RequestMapping("/adminsInsert")
	public String adminsInsert()
	{
		ConstatFinalUtil.LOGGER.info("---- adminsInsert ----");
		return "/back/admins/adminsInsert" ; 
	}
	
	/**
	 * 管理员添加页面提交
	 * 给客户端的响应必须是JSON
	 * { "statusCode":"200", "message":"\u64cd\u4f5c\u6210\u529f", "navTabId":"", "rel":"", "callbackType":"", "forwardUrl":"", "confirmMsg":"" } 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/adminsInsertSubmit",produces = "text/html;charset=UTF-8")
	public String adminsInsertSubmit(HttpServletRequest request,AAdmins admins)
	{
		ConstatFinalUtil.LOGGER.info("---- adminsInsertSubmit ----");
		/* 驱动封装 */
		admins.setPassword(EncryptUtil.encodeStr(admins.getPassword()));
		admins.setCreateTime(new Date());
		admins.setUpdateTime(new Date());
		admins.setLastLoginTime(new Date());
		/* 保存数据库 */
		JSONObject resDbJSON = this.usersService.saveOneAdminsService(admins);
		this.info = resDbJSON.getString("info") ; 
		/* 操作数据库 */
		JSONObject resultJSON = this.printJUIString(request, info);
		return resultJSON.toString() ; 
	}
	
	/**
	 * 退出
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request,HttpSession session)
	{
		ConstatFinalUtil.LOGGER.info("---- logout ----");
		/* 将session中的信息删除 */
		session.removeAttribute("admins");
		session.removeAttribute("lastLoginTime");
		
		/* 操作Token */
		String token = request.getParameter("token");
		if(token == null)
		{
			token = "" ; 
		}
		if(!"".equalsIgnoreCase(token))
		{
			ConstatFinalUtil.TOKENS_MAP.remove(token);
		}
		
		this.info = ConstatFinalUtil.INFO_JSON.getString("8") ;
		/*request.setAttribute("info", info);
		return "/back/login" ; 
		*/
		session.setAttribute("info", info);
		return "redirect:/login.htm" ; 
	}
}
